home *** CD-ROM | disk | FTP | other *** search
- { ========================================================= }
-
- unit Unit1;
-
- interface
-
- uses
- Windows,
- Messages,
- SysUtils,
- Classes,
- Graphics,
- Controls,
- Forms,
- Dialogs,
- ExtCtrls,
- View3D,
- Mat3D,
- Model3D,
- Math3D,
- Ref3D,
- Route3D,
- Text3D;
-
- { ========================================================= }
-
- const
- CylinderRadius : Single = 10.0;
- CylinderHeight : Single = 20.0;
-
- { ========================================================= }
-
- type
- TForm1 = class(TForm)
- View3D1 : TView3D;
- Timer1 : TTimer;
- procedure FormCreate(Sender: TObject);
- procedure FormMouseMove(Sender: TObject;
- Shift: TShiftState; X, Y: Integer);
- procedure FormDestroy(Sender: TObject);
- procedure FormResize(Sender: TObject);
- procedure FormPaint(Sender: TObject);
- procedure Timer1Timer(Sender: TObject);
-
- private
- procedure ConstructCylinder;
- procedure ConstructRoute;
- procedure Draw;
- procedure LoadTextures;
-
- public
- child1 : TView3DReference;
- child2 : TView3DReference;
- material : array [0..11] of TView3DMaterial;
- model : TView3DModel;
- parent : TView3DReference;
- radius : Single;
- texture : array[0..5] of TView3DTexture;
- route : TView3DRoute;
- time : Single;
- end;
-
- { ========================================================= }
-
- var
- Form1 : TForm1;
-
- { ========================================================= }
-
- implementation
-
- {$R *.DFM}
-
- { ======================================================== }
- { === Procedure to draw the view === }
- { ======================================================== }
- procedure TForm1.Draw;
-
- begin
- View3D1.Draw;
- end;
-
- { ======================================================== }
- { === Procedure to construct the view, called when the === }
- { === form is created === }
- { ======================================================== }
- procedure TForm1.FormCreate(Sender: TObject);
-
- begin
- { === create a model === }
- model := TView3DModel.Create;
-
- { === create some references === }
- parent := TView3DReference.Create;
- child1 := TView3DReference.Create;
- child2 := TView3DReference.Create;
-
- { === set the child references to reference the model === }
- child1.Model := @model;
- child2.Model := @model;
-
- { === offset the position of the two child references === }
- child1.Position.X := CylinderRadius;
- child2.Position.X := -CylinderRadius;
-
- { === add the child references to the parent === }
- parent.Add(@child1);
- parent.Add(@child2);
-
- { === load textures === }
- LoadTextures();
-
- { === build a cylinder === }
- ConstructCylinder();
-
- { === calculate polygon vertex normals === }
- model.Smooth(-180.0, 0.0001);
-
- { === add reference to view === }
- View3D1.AddReference(@parent);
-
- { === initialise eye radius === }
- radius := Sqrt(View3D1.Eye.X * View3D1.Eye.X +
- View3D1.Eye.Y * View3D1.Eye.Y +
- View3D1.Eye.Z * View3D1.Eye.Z);
-
- { === construct route === }
- ConstructRoute();
-
- end;
-
- { ======================================================== }
- { === Procedure to load textures === }
- { ======================================================== }
- procedure TForm1.LoadTextures();
-
- var
- bitmap : TBitmap;
- i : Integer;
- name : TFileName;
-
- begin
- { === initialise texture bitmap file name === }
- name := 'textureX.bmp';
-
- { === create a bitmap === }
- bitmap := TBitmap.Create;
-
- { === load six textures === }
- for i := 0 to 5 do begin
- name[8] := Char(i + Integer('1'));
-
- { === create a texture === }
- texture[i] := TView3DTexture.Create;
-
- { === load bitmap from file === }
- bitmap.LoadFromFile(name);
-
- { === assign bitmap to texture === }
- texture[i].Bitmap := bitmap;
-
- { === set texture quality and combination mode === }
- texture[i].Quality := Low;
- texture[i].Combine := Modulate;
- end;
- end;
-
- { ======================================================== }
- { === Procedure to construct a 12-sided cylinder === }
- { ======================================================== }
- procedure TForm1.ConstructCylinder();
-
- var
- angle : Single;
- i : Integer;
- pt : array [0..23] of TView3DPoint3;
- vertex : array [0..3] of TView3DVertex;
-
- begin
- { === setup points for === }
- for i := 0 to 11 do begin
- angle := i * 2.0 * Pi / 12;
- pt[i].x := CylinderRadius * Sin(angle);
- pt[i].y := -CylinderHeight;
- pt[i].z := CylinderRadius * Cos(angle);
- pt[i + 12].x := pt[i].x;
- pt[i + 12].y := CylinderHeight;
- pt[i + 12].z := pt[i].z;
- end;
-
- { === setup polygons === }
- for i := 0 to 11 do begin
-
- { === create a material === }
- material[i] := TView3DMaterial.Create;
-
- { === set material color and texture === }
- material[i].Diffuse.Red := 255;
- material[i].Diffuse.Green := 255;
- material[i].Diffuse.Blue := 255;
- material[i].texture := @texture[i mod 6];
-
- { === setup vertices === }
- vertex[0].point := pt[i];
- vertex[0].texture.x := 0.0;
- vertex[0].texture.y := 0.0;
-
- vertex[1].point := pt[(i + 1) mod 12];
- vertex[1].texture.x := 1.0;
- vertex[1].texture.y := 0.0;
-
- vertex[2].point := pt[(i + 1) mod 12 + 12];
- vertex[2].texture.x := 1.0;
- vertex[2].texture.y := 1.0;
-
- vertex[3].point := pt[i + 12];
- vertex[3].texture.x := 0.0;
- vertex[3].texture.y := 1.0;
-
- { === add polygons (two triangles) to model === }
- model.AddPolygon(vertex[0], vertex[1], vertex[2], @material[i]);
- model.AddPolygon(vertex[0], vertex[2], vertex[3], @material[i]);
- end;
- end;
-
- { ======================================================== }
- { === Procedure to construct a simple route === }
- { ======================================================== }
- procedure TForm1.ConstructRoute();
-
- var
- w : array[0..3] of TView3DRouteWayPt;
-
- begin
- { === create route object === }
- route := TView3DRoute.Create;
-
- { === initialise way points === }
- w[0].x := -50.0;
- w[0].y := 0.0;
- w[0].z := 0.0;
- w[0].pitch := 0.0;
- w[0].yaw := 0.0;
- w[0].roll := 0.0;
- w[0].speed := 25.0;
-
- w[1].x := 0.0;
- w[1].y := 200.0;
- w[1].z := radius;
- w[1].pitch := 0.0;
- w[1].yaw := 90.0;
- w[1].roll := 0.0;
- w[1].speed := 25.0;
-
- w[2].x := 50.0;
- w[2].y := 0.0;
- w[2].z := 0.0;
- w[2].pitch := 0.0;
- w[2].yaw := 180.0;
- w[2].roll := 0.0;
- w[2].speed := 25.0;
-
- w[3].x := 0.0;
- w[3].y := 0.0;
- w[3].z := -radius * 1.5;
- w[3].pitch := 0.0;
- w[3].yaw := 270.0;
- w[3].roll := 0.0;
- w[3].speed := 25.0;
-
- { === add way points === }
- route.Add(w[0]);
- route.Add(w[1]);
- route.Add(w[2]);
- route.Add(w[3]);
-
- { === set route to auto-loop === }
- route.continuous := True;
-
- { === initialise time === }
- time := 0.0;
- end;
-
- { ======================================================== }
- { === Procedure to change the eye point depending on === }
- { === the mouse position === }
- { ======================================================== }
- procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
-
- var
- lat : Single;
- long : Single;
-
- begin
-
- { === calculate yaw value === }
- View3D1.Eye.Yaw := (X - ClientWidth / 2) / ClientWidth * 360.0;
-
- { === calculate pitch value === }
- View3D1.Eye.Pitch := (Y - ClientHeight / 2) / ClientHeight * 180.0;
-
- { === calculate x, y and z values === }
- long := DegToRad(View3D1.Eye.Yaw) + Pi;
- lat := DegToRad(View3D1.Eye.Pitch);
- View3D1.Eye.X := radius * Sin(long) * Cos(lat);
- View3D1.Eye.Y := radius * Sin(lat);
- View3D1.Eye.Z := radius * Cos(long) * Cos(lat);
- end;
-
- { ======================================================== }
- { === Procedure to destroy objects === }
- { ======================================================== }
- procedure TForm1.FormDestroy(Sender: TObject);
-
- var
- i : Integer;
-
- begin
- { === empty model === }
- model.Empty;
-
- { === destroy model === }
- model.Destroy;
-
- { === destroy references === }
- parent.Destroy;
- child1.Destroy;
- child2.Destroy;
-
- { === destroy textures === }
- for i := 0 to 5 do begin
- texture[i].Destroy;
- end;
-
- { === destroy materials === }
- for i := 0 to 11 do begin
- material[i].Destroy;
- end;
-
- { === destroy route === }
- route.Destroy;
- end;
-
- { ======================================================== }
- { === Procedure to redraw the view when the form is === }
- { === resized === }
- { ======================================================== }
- procedure TForm1.FormResize(Sender: TObject);
-
- begin
- { === set new width & height === }
- View3D1.Size.Width := ClientWidth;
- View3D1.Size.Height := ClientHeight;
-
- { === draw === }
- Draw;
- end;
-
- { ======================================================== }
- { === Procedure to redraw the view when the form is === }
- { === repainted === }
- { ======================================================== }
- procedure TForm1.FormPaint(Sender: TObject);
-
- begin
- { === draw === }
- Draw;
- end;
-
- { ======================================================== }
- { === Procedure to handle timer for animation === }
- { ======================================================== }
- procedure TForm1.Timer1Timer(Sender: TObject);
-
- begin
- { === rotate references === }
- child1.Position.Pitch := child1.Position.Pitch + 10.0;
- child2.Position.Yaw := child2.Position.Yaw - 10.0;
-
- { === evaluate route === }
- route.Evaluate(time);
-
- { === inrement time === }
- time := time + 0.1;
-
- { === set parent position === }
- parent.Position := route.Position;
-
- { === draw === }
- Draw();
- end;
-
- end.
-
- { ========================================================= }
-
-
-